<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of mcommon
 *
 * @author Administrator
 */
class Mcommon extends Model {

    //put your code here

    var $row;

    function getRow() {
        return $this->row;
    }

    function findData($table, $whereLike) {
        //$array = array('title' => $match, 'page1' => $match, 'page2' => $match);
        $this->db->like($whereLike);
        //print_r($whereLike);
        $query = $this->db->get($table);
        $data = $query->result_array();
        return $data;
    }

    function getDataPaging($rowPerPage, $offset, $table, $arrWhere, $orderBy, $whereStr="") {
        $this->db->where($arrWhere);
        if($whereStr != ""){
            $this->db->where($whereStr);
        }
        $query = $this->db->get($table);
        $row = $query->result_array();
        $this->row = sizeof($row);
        log_message('info', "row[" . $this->row . "]");
        $offset = $offset - 1;

        log_message('info', "[$rowPerPage][$offset]");
        $this->db->limit($rowPerPage, $offset);
        $this->db->order_by($orderBy[0], $orderBy[1]);
        $this->db->where($arrWhere);
        if($whereStr != ""){
            $this->db->where($whereStr);
        }
        $query = $this->db->get($table);
        $data = $query->result_array();
        return $data;
    }

    function addData($table, $arrData) {
        return $this->db->insert($table, $arrData);
    }

    function delData($table, $arrCond) {
        return $this->db->delete($table, $arrCond);
    }

    function editData($table, $arrData, $arrCond) {
        return $this->db->update($table, $arrData, $arrCond);
    }

    function getDataToSelect($key, $value, $arrWhere, $table, $orderBy){
        $this->db->order_by($orderBy[0], $orderBy[1]);
        $this->db->select("$key,$value");
        //$array = array('name' => $name, 'title' => $title, 'status' => $status);
        $this->db->where($arrWhere);
        // Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active'
        $query = $this->db->get($table);

        $data = array();
        foreach ($query->result_array() as $row) {
            $data[$row[$key]] = $row[$value];
        }
        return $data;
    }

    function getData($table, $arrWhere) {
        //$array = array('name' => $name, 'title' => $title, 'status' => $status);
        $this->db->where($arrWhere);
        // Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active'
        $query = $this->db->get($table);
        $row = $query->result_array();
        return $row;
    }

    function Mcommon() {
        parent::Model();
    }

}

?>
